/*
 * BtAuto - Android Location based Wifi Manager
 * Copyright (C) 2009 Bryan Emmanuel
 * 
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Bryan Emmanuel piusvelte@gmail.com
 */
package com.piusvelte.btauto;

import com.piusvelte.btauto.IBtAutoService.Stub;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class BtAutoService extends Service implements OnSharedPreferenceChangeListener {
	public static final String TAG = "BtAuto";
	private Boolean mCallsonly, mOffondisconnect;
	boolean mBtEnabled;
	private BluetoothAdapter mBtAdapter;
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
				mBtEnabled = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF) == BluetoothAdapter.STATE_ON ? true : false;
			}
		}

	};
	private IUI mIUI;
	private final IBtAutoService.Stub mIBtAutoService = new IBtAutoService.Stub() {
		@Override
		public void setCallback(IBinder binder) throws RemoteException {
			mIUI = IUI.Stub.asInterface(binder);
			if (mIUI != null) {
				try {
					mIUI.bluetoothEnabled(mBtEnabled);
				} catch (RemoteException e) {
					Log.e(TAG,"error updating ui: " + e);
				}
			}
		}

		@Override
		public void enableBluetooth(boolean enable) throws RemoteException {
			if (enable) mBtAdapter.enable();
			else mBtAdapter.disable();
		}
	};

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		init();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStart(intent, startId);
		init();
		return START_STICKY;
	}

	public void init() {
		if (WakeLockManager.hasLock()) WakeLockManager.release();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		SharedPreferences sp = (SharedPreferences) getSharedPreferences(getString(R.string.key_preferences), BtAutoService.MODE_PRIVATE);
		mCallsonly = sp.getBoolean(getString(R.string.key_callsonly), false);
		mOffondisconnect = sp.getBoolean(getString(R.string.key_offondisconnect), false);
		mBtAdapter = BluetoothAdapter.getDefaultAdapter();
		mBtEnabled = mBtAdapter != null ? mBtAdapter.isEnabled() : false;
		IntentFilter i = new IntentFilter();
		i.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
		registerReceiver(mReceiver, i);
	}

	@Override
	public void onDestroy() {
		if (mReceiver != null) {
			unregisterReceiver(mReceiver);
			mReceiver = null;
		}		
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (key.equals(getString(R.string.key_enableservice))) {
			if (!sharedPreferences.getBoolean(key, false)) stopSelf();
		} else if (key.equals(getString(R.string.key_callsonly))) mCallsonly = sharedPreferences.getBoolean(key, false);
		else if (key.equals(getString(R.string.key_offondisconnect))) mOffondisconnect = sharedPreferences.getBoolean(key, false);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return mIBtAutoService;
	}
}
